package com.mauriciobustamante.pruebapds;

import java.io.IOException;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

import com.google.gson.JsonObject;


import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class IngresarPaciente extends Activity {

	private String URL = "http://10.0.2.2:8080/PruebaPDS/services/Clinica";
	private String NAMESPACE = "http://biz.pds.mauriciobustamante.com";
	
	
	private EditText nombre;
	private EditText rut;
	private EditText direccion;
	
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ingresar_paciente);
        
        this.nombre = (EditText) findViewById(R.id.editTextNombre);
        this.rut = (EditText) findViewById(R.id.editTextRut);
        this.direccion = (EditText) findViewById(R.id.editTextDireccion);
        
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_ingresar_paciente, menu);
        return true;
    }
    
    
    public void ingresarPaciente(View v){
    	
    	String nombre = this.nombre.getText().toString();
    	String rut = this.rut.getText().toString();
    	String direccion = this.direccion.getText().toString();
    	// sanitizar entrada
    	
    	if(nombre == null || nombre.equalsIgnoreCase("")){
    		Toast.makeText(IngresarPaciente.this, "Faltan datos - nombre", Toast.LENGTH_LONG).show();
    		return;
    	} 
    	
    	if(rut == null || rut.equalsIgnoreCase("")){
    		Toast.makeText(IngresarPaciente.this, "Faltan datos - rut", Toast.LENGTH_LONG).show();
    		return;
    	}
		
    	if(direccion == null || direccion.equalsIgnoreCase("")){
    		Toast.makeText(IngresarPaciente.this, "Faltan datos - direccion ", Toast.LENGTH_LONG).show();
    		return;
		}
    	
    	JsonObject jsonPaciente = new JsonObject();
    	
		jsonPaciente.addProperty("nombre",nombre);
		jsonPaciente.addProperty("rut", rut);
		jsonPaciente.addProperty("direccion", direccion);
		
		new AgregarPacienteBackground().execute(jsonPaciente.toString());
    }
    
    
    /**
     * Clase que pe
     * 
     * @author micho
     *
     */
    
    class AgregarPacienteBackground extends AsyncTask<String, Void, String>{
		
		private ProgressDialog progressDialog;

		@Override
		protected void onPreExecute() {
    		progressDialog = ProgressDialog.show(
    				IngresarPaciente.this,
    				"Ingresar paciente", 
    				"Espere mientras se ingresa el paciente...", 
    				true);
			super.onPreExecute();
		}
		
		@Override
		protected String doInBackground(String... params) {

			String resultado = new String();
			String METHOD_NAME = "addPaciente";
			String SOAP_ACTION = NAMESPACE +"/" +METHOD_NAME;
			SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
			SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
			soapEnvelope.dotNet = true;
			soapEnvelope.implicitTypes = true;
			soapEnvelope.setOutputSoapObject(request);

			request.addProperty("jsonPaciente", params[0]);

			HttpTransportSE aht = new HttpTransportSE(URL);

			try {
				aht.call(SOAP_ACTION, soapEnvelope);
				SoapPrimitive resultString = (SoapPrimitive)soapEnvelope.getResponse();
				resultado = resultString.toString();

			} catch (IOException e) {
				e.printStackTrace();
			} catch (XmlPullParserException e) {
				e.printStackTrace();
			}

			return resultado;
		}

		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);
			progressDialog.dismiss();
			//Valido que tenga la informacion necesaria
			if("1".equals(result)){
				Toast.makeText(IngresarPaciente.this, "Ingresada correctamente", Toast.LENGTH_LONG).show();
			}else{
				Toast.makeText(IngresarPaciente.this, "Error al obtener la informacion necesaria", Toast.LENGTH_LONG).show();
			}
		}
	}
}
